package it.cpyx.appengine.client;

//import it.cpyx.appengine.client.textarea.RichTextToolbar;

import com.axeiya.gwtckeditor.client.CKConfig;
import com.axeiya.gwtckeditor.client.CKEditor;
import com.axeiya.gwtckeditor.client.Toolbar;
import com.axeiya.gwtckeditor.client.ToolbarLine;
import com.axeiya.gwtckeditor.client.CKConfig.TOOLBAR_OPTIONS;
import com.axeiya.gwtckeditor.client.events.SaveEvent;
import com.axeiya.gwtckeditor.client.events.SaveHandler;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
//import com.google.gwt.user.client.Window;
//import com.google.gwt.user.client.ui.Button;
//import com.google.gwt.user.client.ui.ClickListener;
//import com.google.gwt.user.client.ui.Grid;
//import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
//import com.google.gwt.user.client.ui.Widget;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Cpyx_it0_0_4 implements EntryPoint {
//	/**
//	 * The message displayed to the user when the server cannot be reached or
//	 * returns an error.
//	 */
//	private static final String SERVER_ERROR = "An error occurred while "
//			+ "attempting to contact the server. Please check your network "
//			+ "connection and try again.";
//
//	/**
//	 * Create a remote service proxy to talk to the server-side Greeting service.
//	 */
//	private final GreetingServiceAsync greetingService = GWT
//			.create(GreetingService.class);
//
//	/**
//	 * This is the entry point method.
//	 */
	
//	private String crazyGetSelectedText(RichTextArea rta) {
//        final String MARKER = "http://trevsmarker.com";
//        rta.getFormatter().createLink(MARKER);
//        String withMarker = rta.getHTML();
//
//        int markerIndex = withMarker.indexOf(MARKER);
//        int beginIndex = withMarker.indexOf('>', markerIndex)+1;
//        int endIndex = withMarker.indexOf('<', beginIndex);
//        String selected = withMarker.substring(beginIndex, endIndex);
//
//        rta.getFormatter().removeLink();
//        return selected;
//	}
//
//	private final native void runPretty() /*-{
//	    prettyPrint();
//	}-*/;
	public void onModuleLoad() {
//		final RichTextArea area = new RichTextArea();
//	    area.ensureDebugId("cwRichText-area");
//	    area.setSize("100%", "14em");
//	    RichTextToolbar toolbar = new RichTextToolbar(area);
//	    toolbar.ensureDebugId("cwRichText-toolbar");
//	    toolbar.setWidth("100%");
//
//	    // Add the components to a panel
//	    
//	    
//	    Button btn=new Button("alertText");
//	    btn.addClickListener(new ClickListener() {
//			
//			@Override
//			public void onClick(Widget sender) {
////				crazyGetSelectedText(area);
//				area.getFormatter().insertHTML("<b>"+crazyGetSelectedText(area)+"</b>");
//				runPretty();
//			}
//		});
//	    Grid grid = new Grid(3, 1);
//	    grid.setStyleName("cw-RichText");
//	    grid.setWidget(0, 0, toolbar);
//	    grid.setWidget(1, 0, area);
//	    grid.setWidget(2, 0, btn);
//	    grid.setBorderWidth(3);
//	    RootPanel.get().add(grid);
        CKConfig CONFIG_MODIFICATION = new CKConfig();
        // Creating personalized toolbar
        ToolbarLine line = new ToolbarLine();
        // Define the first line
        TOOLBAR_OPTIONS[] t1 = { TOOLBAR_OPTIONS.Save, TOOLBAR_OPTIONS._,
                        TOOLBAR_OPTIONS.Cut, TOOLBAR_OPTIONS.Copy,TOOLBAR_OPTIONS.Paste, TOOLBAR_OPTIONS.PasteText,TOOLBAR_OPTIONS.PasteFromWord, TOOLBAR_OPTIONS._,
                        TOOLBAR_OPTIONS.SpellChecker, TOOLBAR_OPTIONS.Scayt,TOOLBAR_OPTIONS._, 
                        TOOLBAR_OPTIONS.Undo, TOOLBAR_OPTIONS.Redo,TOOLBAR_OPTIONS._, 
                        TOOLBAR_OPTIONS.Find,TOOLBAR_OPTIONS.Replace, TOOLBAR_OPTIONS._ };
        line.addAll(t1);

        // Define the second line
        ToolbarLine line2 = new ToolbarLine();
        TOOLBAR_OPTIONS[] t2 = { 
                        TOOLBAR_OPTIONS.Bold, TOOLBAR_OPTIONS.Italic,TOOLBAR_OPTIONS.Underline, TOOLBAR_OPTIONS.Strike,
                        TOOLBAR_OPTIONS.Subscript, TOOLBAR_OPTIONS.Superscript, TOOLBAR_OPTIONS._, 
                        TOOLBAR_OPTIONS.NumberedList,TOOLBAR_OPTIONS.BulletedList, TOOLBAR_OPTIONS._,
                        TOOLBAR_OPTIONS.Outdent, TOOLBAR_OPTIONS.Indent,TOOLBAR_OPTIONS.Blockquote, TOOLBAR_OPTIONS._,
                        TOOLBAR_OPTIONS.JustifyLeft, TOOLBAR_OPTIONS.JustifyCenter,     TOOLBAR_OPTIONS.JustifyRight, TOOLBAR_OPTIONS.JustifyBlock,     TOOLBAR_OPTIONS._,
                        TOOLBAR_OPTIONS.Image,TOOLBAR_OPTIONS.Table, TOOLBAR_OPTIONS.HorizontalRule,TOOLBAR_OPTIONS.SpecialChar };
        line2.addAll(t2);

        //define the third line
        ToolbarLine line3 = new ToolbarLine();
        TOOLBAR_OPTIONS[] t3 = { 
                        TOOLBAR_OPTIONS.Styles, TOOLBAR_OPTIONS.Format, TOOLBAR_OPTIONS.Font, TOOLBAR_OPTIONS.FontSize, TOOLBAR_OPTIONS._,
                        TOOLBAR_OPTIONS.TextColor, TOOLBAR_OPTIONS.BGColor,     TOOLBAR_OPTIONS._,
                        TOOLBAR_OPTIONS.ShowBlocks };
        line3.addAll(t3);

        // Creates the toolbar
        Toolbar t = new Toolbar();
        t.add(line);
        t.addSeparator();
        t.add(line2);
        t.addSeparator();
        t.add(line3);

        // sets some params
        CONFIG_MODIFICATION.setResizeMinWidth(520);
        CONFIG_MODIFICATION.setResizeMinHeight(250);
        CONFIG_MODIFICATION.setResizeMaxWidth(1600);
        CONFIG_MODIFICATION.setResizeMaxHeight(1600);
        CONFIG_MODIFICATION.setEntities_latin(false);
        CONFIG_MODIFICATION.setEntities(false);
        CONFIG_MODIFICATION.setWidth(Window.getClientWidth() / 2 + "");
        // Set the toolbar to the config (replace the FULL preset toolbar)
        CONFIG_MODIFICATION.setToolbar(t);
        
        final CKEditor ckec = new CKEditor(CONFIG_MODIFICATION);
        ckec.addSaveHandler(new SaveHandler<CKEditor>() {
		    @Override
		    public void onSave(SaveEvent<CKEditor> event) {
		        CKEditor target = event.getTarget();//The CKEditor which generates the save event
		        String content = event.getText();//The content to save (also available in target)
		        final DialogBox dialogBox=new DialogBox();
		        dialogBox.setText("Remote Procedure Call");
				dialogBox.setAnimationEnabled(true);
				final Button closeButton = new Button("Close");
				closeButton.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						dialogBox.hide();

					}
				});
				final HTML serverResponseLabel = new HTML(event.getText());
				VerticalPanel dialogVPanel = new VerticalPanel();
				dialogVPanel.addStyleName("dialogVPanel");
				dialogVPanel.add(new HTML("<b>Sending name to the server:</b>"));
				dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
				dialogVPanel.add(serverResponseLabel);
				dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
				dialogVPanel.add(closeButton);
				dialogBox.setWidget(dialogVPanel);
				dialogBox.show();
		    }
		});
        RootPanel.get().add(ckec);

	    
//		final Button sendButton = new Button("Send");
//		final TextBox nameField = new TextBox();
//		nameField.setText("GWT User");
//
//		// We can add style names to widgets
//		sendButton.addStyleName("sendButton");
//
//		// Add the nameField and sendButton to the RootPanel
//		// Use RootPanel.get() to get the entire body element
//		RootPanel.get("nameFieldContainer").add(nameField);
//		RootPanel.get("sendButtonContainer").add(sendButton);
//
//		// Focus the cursor on the name field when the app loads
//		nameField.setFocus(true);
//		nameField.selectAll();
//
//		// Create the popup dialog box
//		final DialogBox dialogBox = new DialogBox();
//		dialogBox.setText("Remote Procedure Call");
//		dialogBox.setAnimationEnabled(true);
//		final Button closeButton = new Button("Close");
//		// We can set the id of a widget by accessing its Element
//		closeButton.getElement().setId("closeButton");
//		final Label textToServerLabel = new Label();
//		final HTML serverResponseLabel = new HTML();
//		VerticalPanel dialogVPanel = new VerticalPanel();
//		dialogVPanel.addStyleName("dialogVPanel");
//		dialogVPanel.add(new HTML("<b>Sending name to the server:</b>"));
//		dialogVPanel.add(textToServerLabel);
//		dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
//		dialogVPanel.add(serverResponseLabel);
//		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
//		dialogVPanel.add(closeButton);
//		dialogBox.setWidget(dialogVPanel);
//
//		// Add a handler to close the DialogBox
//		closeButton.addClickHandler(new ClickHandler() {
//			public void onClick(ClickEvent event) {
//				dialogBox.hide();
//				sendButton.setEnabled(true);
//				sendButton.setFocus(true);
//			}
//		});
//
//		// Create a handler for the sendButton and nameField
//		class MyHandler implements ClickHandler, KeyUpHandler {
//			/**
//			 * Fired when the user clicks on the sendButton.
//			 */
//			public void onClick(ClickEvent event) {
//				sendNameToServer();
//			}
//
//			/**
//			 * Fired when the user types in the nameField.
//			 */
//			public void onKeyUp(KeyUpEvent event) {
//				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
//					sendNameToServer();
//				}
//			}
//
//			/**
//			 * Send the name from the nameField to the server and wait for a response.
//			 */
//			private void sendNameToServer() {
//				sendButton.setEnabled(false);
//				String textToServer = nameField.getText();
//				textToServerLabel.setText(textToServer);
//				serverResponseLabel.setText("");
//				greetingService.greetServer(textToServer,
//						new AsyncCallback<String>() {
//							public void onFailure(Throwable caught) {
//								// Show the RPC error message to the user
//								dialogBox
//										.setText("Remote Procedure Call - Failure");
//								serverResponseLabel
//										.addStyleName("serverResponseLabelError");
//								serverResponseLabel.setHTML(SERVER_ERROR);
//								dialogBox.center();
//								closeButton.setFocus(true);
//							}
//
//							public void onSuccess(String result) {
//								dialogBox.setText("Remote Procedure Call");
//								serverResponseLabel
//										.removeStyleName("serverResponseLabelError");
//								serverResponseLabel.setHTML(result);
//								dialogBox.center();
//								closeButton.setFocus(true);
//							}
//						});
//			}
//		}
//
//		// Add a handler to send the name to the server
//		MyHandler handler = new MyHandler();
//		sendButton.addClickHandler(handler);
//		nameField.addKeyUpHandler(handler);
	}
}
